#coding: utf-8
import urllib2, cookielib, urllib, yaml, json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

class salt_api_token:
    def __init__(self, data, url, token=None):
        self.data = data
        self.url = url
        self.headers = {
            'User-agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
            "Accept": "application/x-yaml",
        }
        if token:
            self.headers.update(token)

    def run(self):
        req = requests.post(self.url, headers=self.headers, data=self.data, verify=False)
        context = req.text
        return yaml.load(context)
class salt_api_jobs(object):
    def __init__(self, url, token=None):
        self.url = url
        self.headers = {
            'User-agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
            "Accept":"application/x-yaml",
        }
        if token:
            self.headers.update(token)

    def run(self):
        context = urllib2.Request(self.url, headers=self.headers)
        resp = urllib2.urlopen(context)
        context = resp.read()
        return yaml.load(context)

class pxe_api(object):
    """pxe api接口"""
    def __init__(self, data, pxe_url):
        self.data = data
        self.url = pxe_url
        self.headers = {
            'User-agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
            'Referer': 'http://auto.optools.int.jumei.com'
        }

    def run(self):
        """执行"""
        try:
            pxe_content = requests.post(self.url, data=self.data, headers=self.headers)
        except requests.ConnectionError:
            pxe_content_data = {'status': 110, 'result': "pxe接口请求失败,请通知管理员检查接口请况" }
            return json.dumps(pxe_content_data)
        pxe_content_data = pxe_content.text
        return pxe_content_data

class pxe_api_delete(object):
    """通知pxe删除接口"""
    def __init__(self, mac, pxe_url):
        self.data = mac
        self.url = pxe_url
        self.headers = {
            'User-agent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
        }

    def run(self):
        pxe_content = requests.post(self.url, data=self.data)
        pxe_content_data = pxe_content.text
        return pxe_content_data

class Salt_Jobsid(object):
    def __init__(self, data):
        self.data = data
    def run(self):
        content = {}
        for i in self.data["return"]:
            content["jid"] = i["jid"]
        return content

# if __name__ =="__main__":
#     data = {'username':'xiaoluo','password':'123456',"eauth":"pam"}
#     url='https://10.43.1.205:8000/login'
#     # test=salt_api_token(data,url)
#     # key =  test.run()
#     # token = key['return'][0]['token']
#     token = '87dc0ae01dc27a6e834b09845dda75adb31f78d9'
#     # data={"client": 'local',"tgt": "GF10-43-1-206",'fun':'grains.items'}
#     # url='https://10.43.1.205:8000'
#     # test = salt_api_jobs(data,url,{"X-Auth-Token": token})
#     # print test.run()
#     url='https://10.43.1.205:8000'
#     xToken={'X-Auth-Token' : token}
#     data = {'client':'local','fun': 'cmd.run', 'tgt': '*', 'arg': 'ls -l'}
#     test = salt_api_token(data, url,xToken)
#     print test.run()
